package com.bc004346.travelus.dataAccess.asyncTasks;

import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;

import com.bc004346.travelus.dataAccess.TravelUsScreenScraper;
import com.bc004346.travelus.model.domain.Passenger;
import com.bc004346.travelus.model.interfaces.ISelectPassengersCallback;
import com.bc004346.travelus.utility.Utility;

import java.util.ArrayList;
import java.util.List;

import static com.bc004346.travelus.utility.Utility.log;

/**
 * Created by Renats on 3/12/14.
 * Search for flights
 */
public class GetAvailablePassengersTaskFragment extends Fragment {

    //<editor-fold desc="Members">
    private static final String ORIGIN = "origin";
    private static final String DESTINATION = "destination";
    private static final String DEPARTURE = "departure";
    private ISelectPassengersCallback mCallbacks;
    private TravelUsScreenScraper mTravelUS;
    private String mOrigin;
    private String mDestination;
    private String mDeparture;
    //</editor-fold>

    //<editor-fold desc="Static Methods">
    public static GetAvailablePassengersTaskFragment newInstance(String origin, String destination, String departure) {
        GetAvailablePassengersTaskFragment f = new GetAvailablePassengersTaskFragment();
        Bundle args = new Bundle();
        args.putString(ORIGIN, origin);
        args.putString(DESTINATION, destination);
        args.putString(DEPARTURE, departure);
        f.setArguments(args);
        return f;
    }
    //</editor-fold>

    //<editor-fold desc="Overrides">
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mCallbacks = (ISelectPassengersCallback) activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        mTravelUS = Utility.getInstance().getTravelUS();
        mOrigin = getArguments() == null ? "" : getArguments().getString(ORIGIN);
        mDestination = getArguments() == null ? "" : getArguments().getString(DESTINATION);
        mDeparture = getArguments() == null ? "" : getArguments().getString(DEPARTURE);
        new SelectPassengersTask().execute();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }
    //</editor-fold>

    private class SelectPassengersTask extends AsyncTask<Void, Void, List<Passenger>> {

        private Exception ex = null;

        @Override
        protected void onPostExecute(List<Passenger> param) {
            if (mCallbacks != null) {
                mCallbacks.onSelectPassengersPostExecute(param, ex);
            }
        }

        @Override
        protected List<Passenger> doInBackground(Void... params) {

            List<Passenger> passengers = new ArrayList<Passenger>();
            try {
                passengers = mTravelUS.getAvailablePassengers(mOrigin, mDestination, mDeparture);
            } catch (Exception e) {
                log(String.format("Exception: %s", e.toString()));
                ex = e;
            }
            return passengers;
        }
    }
}
